package mysql

import "common/models"

func (m Mysql) ImgInfoCheckByUid(uid string) (b bool, Id int, err error) {
	var (
		count   int64
		imgInfo *models.ImageInfo
	)
	b = false
	err = m.Db.Table("image_info").Where("image_id = ?", uid).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		m.Db.Table("image_info").Where("image_id = ?", uid).First(&imgInfo)
		Id = int(imgInfo.ID)
		b = true
		return
	}
	return
}
func (m Mysql) ImgInfoAdd(img *models.ImageInfo) (err error) {
	return m.Db.Table("image_info").Create(&img).Error
}

func (m Mysql) ImgInfoQueryByUid(uid string) (res *models.ImageInfo, err error) {
	err = m.Db.Table("image_info").Where("image_id = ?", uid).First(&res).Error
	return
}
func (m Mysql) ImgInfoQueryById(id string) (res *models.ImageInfo, err error) {
	err = m.Db.Table("image_info").Where("id = ?", id).First(&res).Error
	return
}
func (m Mysql) ImgInfoQueryByAuthId(authId string, page, size int) (res []*models.ImageInfo, total int64, err error) {
	err = m.Db.Table("image_info").Where("author_id = ?", authId).Offset((page - 1) * size).Limit(size).Find(&res).Count(&total).Error
	return
}
func (m Mysql) ImgInfoSaveByUid(img *models.ImageInfo) (err error) {
	err = m.Db.Table("image_info").Where("image_id = ?", img.ImageId).Save(&img).Error
	return
}
func (m Mysql) ImgInfoDelByUid(uid string) (src string, err error) {
	var res *models.ImageInfo
	err = m.Db.Table("image_info").Unscoped().Where("image_id = ?", uid).First(&res).Delete(&models.ImageInfo{}).Error
	res.Src = src
	return
}
